/**   
* @Title: SpaService.java 
* @Package com.hw.one.peach.core.service 
* @Description: TODO() 
* @author songjundan   
* @date 2017年8月22日 下午3:27:43 
* @version V1.0   
*/ 
package com.hw.one.peach.core.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.peach.core.dao.ISpaDao;
import com.hw.one.peach.core.param.SpaParam;
import com.hw.one.peach.core.po.SpaBasePO;
import com.hw.one.peach.core.po.SpaPO;

/**
  * @author  sjd 
  * @date 创建时间：2017年8月22日 下午3:27:43 
  * @version 1.0.0
  */
/**
 * @author Administrator
 *
 */
@Service(value = "spaService")
public class SpaService implements ISpaService {

	@Autowired
	private ISpaDao spaDao;
	
	@Override
	public  MsgResult getSpaHourDataByTime(Map<String, Object> paraMap) {
		 // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        String serialNumber = paraMap.get("monitorCode").toString();
        
        SpaParam spaParam = new SpaParam();
        spaParam.setStartTime(startTime);
        spaParam.setEndTime(endTime);
        spaParam.setSerialNumber("AT1501G0054S");
        List<SpaPO> SpaPOList = spaDao.getSpaHourDataByTime(spaParam);
        List<String> timeDetailList = null;
        List<String> elementNameList = new ArrayList<>();
        List<String> elementColorList = new ArrayList<>();
        List<String> sourcesNameList = new ArrayList<>();
        List<String> sourcesColorList = new ArrayList<>();
        List<Map<String,Object>> elelist = new ArrayList<>();  //要返回的污染元素的数据
        List<Map<String,Object>> sourcesList = new ArrayList<>();//要返回的污染源的数据
        List<SpaBasePO> elements;
        List<SpaBasePO> sources;
        // 因为抓取数据是按照hour时间，直接安装hour时间截取
        int timeLen = 16;
        
       /* {
            name:'邮件营销',
            type:'line',
            stack: '总量',
            areaStyle: {normal: {}},
            data:[120, 132, 101, 134, 90, 230, 210]
        }*/
        String time ;
		String[] eledateStr = null;
		String[] sourceStr = null;
		timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 14) + "00", endTime.substring(0, 14) + "00", "hour");
       // System.out.println(SpaPOList.size());
        if(SpaPOList==null||SpaPOList.size()<=0){
        	System.out.println("数据为空");
        	return MsgResult.okResult("暂无数据");
        }else{
        	eledateStr = new String[SpaPOList.get(0).getELEMENT().size()];
			sourceStr = new String[SpaPOList.get(0).getSOURCES().size()];
        	for(int j=0;j<SpaPOList.size();j++){
    			SpaPO spaPO =  SpaPOList.get(j);
				elements = spaPO.getELEMENT();//获取因子的数组  17个因子
				sources = spaPO.getSOURCES();
    	        	/*time = spaPO.getMONITORTIME(); //获取监控时间
            		time = time.substring(0, timeLen);*/
            	
            		//取element的数据
            			for(int i=0;i<elements.size();i++){
            				if(j==0){
            					Map<String,Object> e = new HashMap<>();
            					
                				e.put("name", elements.get(i).getName() == null ? "-" : elements.get(i).getName());//放名称
                				e.put("color",elements.get(i).getColor() == null ? "-" : elements.get(i).getColor());
                				elelist.add(i, e);
            				}
            				if(eledateStr[i]==null){
            					eledateStr[i]="";
            				}
            				 eledateStr[i] = eledateStr[i]+(elements.get(i).getData()== null ? "-" :elements.get(i).getData()) +",";
        	        	}
            			
            			//取sources的数据
            			for(int i=0;i<sources.size();i++){
            				if(j==0){
            					Map<String,Object> e = new HashMap<>();
                				e.put("name", sources.get(i).getName() == null ? "-" :sources.get(i).getName());//放名称
                				e.put("color",sources.get(i).getColor() == null ? "-" :sources.get(i).getColor());
                				sourcesList.add(i, e);
            				}
            				if(sourceStr[i]==null){
            					sourceStr[i]="";
            				}
            				sourceStr[i] = sourceStr[i]+(sources.get(i).getData() == null ? "-":sources.get(i).getData()) +",";
            			}
            			
            			
			}
    		JSONArray dataArrElement = new JSONArray();
    		//遍历出组装的element的data放入对应的map中
			if (eledateStr !=null) {
				for(int i=0;i<elelist.size();i++){
					eledateStr[i]= eledateStr[i].substring(0, eledateStr[i].length()-1);
					String[] data = eledateStr[i].split(",");
					elementNameList.add(elelist.get(i).get("name").toString());
					elementColorList.add(elelist.get(i).get("color").toString());
					dataArrElement.add(data);
				}
			}
        	JSONArray dataArrSources = new JSONArray();
			if (sourceStr != null) {
				//遍历出组装的source的data放入对应的map中
				for(int i=0;i<sourcesList.size();i++){
					sourceStr[i]= sourceStr[i].substring(0, sourceStr[i].length()-1);
					String[] dataSources = sourceStr[i].split(",");
					sourcesNameList.add(sourcesList.get(i).get("name").toString());
					sourcesColorList.add(sourcesList.get(i).get("color").toString());
					dataArrSources.add(dataSources);
				}
			}
        	JSONObject resultObj = new JSONObject();
        	
            resultObj.put("timeList", timeDetailList);
            resultObj.put("elementsDataList", dataArrElement);	
            resultObj.put("elementNameList", elementNameList);
            resultObj.put("elementColorList", elementColorList);	
            resultObj.put("sourcesNameList", sourcesNameList);	
            resultObj.put("sourcesColorList", sourcesColorList);	
            resultObj.put("sourcesDataList", dataArrSources);
            return MsgResult.okResult(resultObj);
        }
        
    		
		
	}

	
	
}
